package com.loveprogrammer.eternity.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @version 1.0.0
 * @description:
 * @author: eric
 * @date: 2022-09-07 14:16
 **/
public class ExcelUtil {

    private static List<String> strKeys = Arrays.asList("name","skills","extSkills");
    private static List<String> intKeys = Arrays.asList("id","level","weaponType");
    /**
     * JSONArray转Excel表格
     * 表格的数据列数取自于JSONArray第一个对象的所有key值，所JSONArray的第一行数据必须是全的，否则会导致部分数据列显示不出来
     * @param jsonArray
     * @param excelFilePath
     */
    public static void jsonToExcel(JSONArray jsonArray, String excelFilePath) {
        try{
            //此处为你的json数组
            HSSFWorkbook sheets = jsonToExcel(jsonArray);
            // 文件下载
            File excelFile = new File(excelFilePath);
            sheets.write(excelFile);
            sheets.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * json 转 excel
     * @param jsonArray
     * @return
     * @throws
     */
    private static HSSFWorkbook jsonToExcel(JSONArray jsonArray) {
        Set<String> keys = null;
        // 创建HSSFWorkbook对象
        HSSFWorkbook wb = new HSSFWorkbook();
        // 创建HSSFSheet对象
        HSSFSheet sheet = wb.createSheet("sheet0");

        int roleNo = 0;
        int rowNo = 0;
        // 创建HSSFRow对象
        HSSFRow row = sheet.createRow(roleNo++);
        // 创建HSSFCell对象
        // 创建表格标题
        keys = jsonArray.getJSONObject(0).keySet();
        for (String s : keys) {
            HSSFCell cell = row.createCell(rowNo++);
            cell.setCellValue(s);
        }
        rowNo = 0;

        for (int i = 0; i < jsonArray.size(); i ++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);

            row = sheet.createRow(roleNo++);
            for (String s : keys) {
                HSSFCell cell = row.createCell(rowNo++);
                if(intKeys.contains(s)) {
                    cell.setCellValue(jsonObject.getIntValue(s));
                }else{
                    cell.setCellValue(jsonObject.getString(s));
                }
            }
            rowNo = 0;
        }

        return wb;
    }

    public static void main(String[] args) throws IOException {
//        JSONArray jsonArray = new JSONArray();
//        JSONObject o1 = new JSONObject();
//        o1.put("screenWidth", 1280);
//        o1.put("screenHight", 2270);
//        o1.put("trackState", 1);
//        jsonArray.add(o1);
//        JSONObject o2 = new JSONObject(new LinkedHashMap<>());
//        o2.put("screenWidth", 1270);
//        o2.put("trackState", 2);
//        jsonArray.add(o2);
//        JSONObject o3 = new JSONObject(new LinkedHashMap<>());
//        o3.put("screenHight", 1290);
//        o3.put("trackState", 3);
//        jsonArray.add(o3);

        ClassLoader classLoader = ExcelUtil.class.getClassLoader();
        // InputStream inputStream = classLoader.getResourceAsStream("equipments/weapon.json");
        InputStream inputStream = classLoader.getResourceAsStream("job.json");
        assert inputStream != null;
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(inputStream, StandardCharsets.UTF_8));
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);

        JSONArray jsonArray = JSONArray.parseArray(responseStrBuilder.toString());

        // intKeys = Arrays.asList("id","level","weaponType");
        // intKeys = Arrays.asList("id","purchase","type","upgradeLevel","prosperity","topography","storeId");

        intKeys = Arrays.asList("id","highestLevel","strength","armature","constitution","magic","technique","speed","levelUpStrength","levelUpArmature","levelUpConstitution","levelUpMagic","levelUpTechnique",
                "levelUpSpeed","attackType","weaponType","position");
        jsonToExcel(jsonArray, "job.xls");
    }

}
